#include "BaseType.h"
#include "Singleton.h"
#include "Tracer.h"

using namespace libemb;

/**
 * 简单工厂：
 * 1.一个抽象产品类,可以派生多个具体产品类
 * 2.只有一个工厂类(没有抽象工厂)
 * 3.工厂根据参数创建不同的具体产品
 * 4.每增加一种具体产品，就必须修改工厂类来实现创建该产品的方法
 */

//抽象产品:Car
class Car{
DECL_CLASSNAME(Car)
public:
    virtual void run()=0;
};

//汽油车
class GasolineCar:public Car{
DECL_CLASSNAME(GasolineCar)
public:
    void run() override
    {
        TRACE_DBG_CLASS("I am Gasoline Car, I run with gas!");
    }
};

//电动车
class ElectricCar:public Car{
DECL_CLASSNAME(ElectricCar) 
public:
void run() override
    {
        TRACE_DBG_CLASS("I am Electric Car, I run with battery!");
    }
};

//汽车制造工厂,单例模式
class CarFactory:public Singleton<CarFactory>{
DECL_CLASSNAME(CarFactory)
DECL_SINGLETON(CarFactory)
public:
    enum CarType{
        GASOLINE_CAR=0,
        ELECTRIC_CAR
    };
public:
    //生产汽车的方法
    std::shared_ptr<Car> createCar(int type)
    {
        switch (type){
        case GASOLINE_CAR:
            return std::make_shared<GasolineCar>();
        case ELECTRIC_CAR:
            return std::make_shared<ElectricCar>();
        default:
            return nullptr;
        }
    }
};

CarFactory::CarFactory()
{
}

int main()
{
    Tracer::getInstance().setLevel(TRACE_LEVEL_DBG);
	Tracer::getInstance().addSink(std::make_shared<STDSink>()).start();
    auto gasoline = CarFactory::getInstance().createCar(CarFactory::GASOLINE_CAR);
    if (gasoline)
    {
        gasoline->run();
    }
    auto electric = CarFactory::getInstance().createCar(CarFactory::ELECTRIC_CAR);
    if (electric)
    {
        electric->run();
    }
    return RC_OK;
}